#include "stdafx.h"
#include "vol_user_app_info.h"

CV8PlatformWrapper::CV8PlatformWrapper() {}
CV8PlatformWrapper::~CV8PlatformWrapper() {}

void CV8PlatformWrapper::CreatePlatform(int thread_pool_size, BOOL idle_task_support, BOOL in_process_stack_dumping)
{
	if (m_platform != nullptr)
		return;
	if (thread_pool_size < 0)
	{
		m_platform = platform::NewSingleThreadedDefaultPlatform(
			idle_task_support ? platform::IdleTaskSupport::kEnabled : platform::IdleTaskSupport::kDisabled,
			in_process_stack_dumping ? platform::InProcessStackDumping::kEnabled : platform::InProcessStackDumping::kDisabled
		);
	}else{
		m_platform = platform::NewDefaultPlatform(thread_pool_size,
			idle_task_support ? platform::IdleTaskSupport::kEnabled : platform::IdleTaskSupport::kDisabled,
			in_process_stack_dumping ? platform::InProcessStackDumping::kEnabled : platform::InProcessStackDumping::kDisabled
		);
	}
}

Platform* CV8PlatformWrapper::GetPlatform()
{
	if (m_platform == nullptr)
		CreatePlatform();
	return m_platform.get();
}

CV8IsolateWrapper::CV8IsolateWrapper()
{
	m_isolate = NULL;
	pWrapper = NULL;
	autoDestroyIsolate = TRUE;
}

CV8IsolateWrapper::~CV8IsolateWrapper()
{
	DestroyIsolate();
}

void CV8IsolateWrapper::CreateIsolate(Isolate::CreateParams& stParams)
{
	ASSERT (m_isolate == NULL && pWrapper == NULL);	//已被成功创建?
	m_isolate = v8::Isolate::New(stParams);
	pWrapper = new IsolateWrapper(m_isolate);
}

void CV8IsolateWrapper::CreateIsolateWithoutWrapper(Isolate* pIsolate)
{
	ASSERT (m_isolate == NULL && pWrapper == NULL);	//已被成功创建?
	this->m_isolate = pIsolate;
	autoDestroyIsolate = FALSE;
}

Isolate* CV8IsolateWrapper::GetIsolate()
{
	ASSERT(m_isolate != NULL); 
	return m_isolate;
}

HandleScope& CV8IsolateWrapper::GetHandleScope()
{
	ASSERT(m_isolate != NULL && pWrapper != NULL); 
	return pWrapper->m_HandleScope;
}

Isolate::Scope& CV8IsolateWrapper::GetScope()
{
	ASSERT(m_isolate != NULL && pWrapper != NULL); 
	return pWrapper->m_Scope;
}

void CV8IsolateWrapper::DestroyIsolate()
{
	if (autoDestroyIsolate)
	{
		if (pWrapper != NULL) delete pWrapper;
		if (m_isolate != NULL) m_isolate->Dispose();
	}
}